    //
//  RegisterViewController.m
//  AirTicket
//
//  Created by imac.bmw on 11-1-6.
//  Copyright 2011 aircococ. All rights reserved.
//

#import "RegisterViewController.h"
#import "CWUserRegisterInfo.h"
#import "extver.h"
#import "RootViewController.h"

@implementation RegisterViewController

// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
    }
    return self;
}
*/
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
	
	[textField resignFirstResponder];
	//NSLog(@"name = %@",nameText.text);
	return YES;
}

-(void)toRegister {

	if (!cellphone.text || !loginpassword.text || !personID.text || !nameText.text || !sexSelection) {
		UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"出错了" message:@"缺少必要信息" delegate:self cancelButtonTitle:@"好的" otherButtonTitles:nil];
		
		[alert show];
		[alert release];
	}
	else {
		CWUserRegisterInfo *uri = [[CWUserRegisterInfo alloc] init];
		[uri initInfoList];
		[uri setUserID:cellphone.text];
		[uri setPassword:loginpassword.text];
		[uri setIDNo:personID.text];
		[uri setUserName:nameText.text];
		[uri setSex:sexSelection];
		[uri setMobil:cellphone.text];
		
		if (email.text != NULL) {
			[uri setEmail:email.text];
		}
		
		
		if (telephone.text != NULL) {
			[uri setPhone:telephone.text];
		}
		if (payaddress.text != NULL) {
			[uri setAddress:payaddress.text];
		}
		if (postcode.text!= NULL) {
			[uri setCode:postcode.text];
		}
		
		
		if (question1.text!= NULL) {
			[uri setPSQ1:question1.text];
		}
		if (answer1.text!= NULL) {
			[uri setPSA1:answer1.text];
		}
		if (question2.text!= NULL) {
			[uri setPSQ2:question2.text];
		}
		if (answer2.text!= NULL) {
			[uri setPSA2:answer2.text];
		}
		
		
		[clientSocket sendRegister:uri];
		[uri release];
	}

}

// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
	[super loadView];
	
//	clientSocket = [[CWSocketByStream alloc] init];
//	[clientSocket createStream:@"58.247.98.94" serverPort:7000];
	
	UIBarButtonItem *reg = [[UIBarButtonItem alloc]initWithTitle:@"注册" 
														   style:UIBarButtonItemStylePlain 
														  target:self 
														  action:@selector(toRegister)];
	self.navigationItem.rightBarButtonItem = reg;
	[reg release];
	
	UIImage *img = [UIImage imageNamed:@"loginback"];
	UIImageView *regBack = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, img.size.width, img.size.height)];
	regBack.image = img;
	[self.view addSubview:regBack];
	[regBack release];
	
	UIScrollView *content = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 800)];
	content.contentSize = CGSizeMake(320, 1200);
	content.scrollEnabled = YES;
	[self.view addSubview:content];
	[content release];
	
	UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(5, 15, 90, 20)];
	label.text = @"姓名";
	[label setTag:1];
	label.font = [UIFont systemFontOfSize:13];
	label.textAlignment = UITextAlignmentRight;
	label.backgroundColor = [UIColor clearColor];
	[content addSubview:label];
	[label release];
	
	nameText = [[UITextField alloc]initWithFrame:CGRectMake(100, 15, 100, 20)];
	nameText.borderStyle = UITextBorderStyleRoundedRect;
	nameText.placeholder = @"必填";
	nameText.returnKeyType = UIReturnKeyDone;
	nameText.delegate = self;
	[content addSubview:nameText];
	[nameText release];
	
	label = [[UILabel alloc]initWithFrame:CGRectMake(5, 60, 90, 20)];
	label.text = @"性别(必选)";
	label.textAlignment = UITextAlignmentRight;
	label.font = [UIFont systemFontOfSize:13];
	label.backgroundColor = [UIColor clearColor];
	[content addSubview:label];
	[label release];
	
	UISegmentedControl *sex = [[UISegmentedControl alloc]initWithItems:nil];
	sex.frame = CGRectMake(100, 60, 100, 20);
	[sex setTag:900];
	sex.segmentedControlStyle = UISegmentedControlStylePlain;
	sex.tintColor = [UIColor colorWithRed:0.75 green:1.0 blue:0.75 alpha:1.0];
	[sex insertSegmentWithTitle:@"男" atIndex:0 animated:NO];
	[sex insertSegmentWithTitle:@"女" atIndex:1 animated:NO];
	sex.selectedSegmentIndex = 0;
	sexSelection = @"1";
	[sex addTarget:self action:@selector(selectSex) forControlEvents:UIControlEventValueChanged];
	[content addSubview:sex];
	[sex release];
	
	
	//---------------------------------
	label = [[UILabel alloc]initWithFrame:CGRectMake(5, 105, 90, 20)];
	label.text = @"身份证号";
	label.textAlignment = UITextAlignmentRight;
	label.font = [UIFont systemFontOfSize:13];
	label.backgroundColor = [UIColor clearColor];
	[content addSubview:label];
	[label release];
	
	personID = [[UITextField alloc]initWithFrame:CGRectMake(100, 105, 100, 20)];
	personID.borderStyle = UITextBorderStyleRoundedRect;
	personID.placeholder = @"必填";
	personID.returnKeyType = UIReturnKeyDone;
	personID.delegate = self;
	[content addSubview:personID];
	[personID release];
	//---------------------------------------------
	label = [[UILabel alloc]initWithFrame:CGRectMake(5, 150, 90, 20)];
	label.text = @"电子邮件";
	label.textAlignment = UITextAlignmentRight;
	label.font = [UIFont systemFontOfSize:13];
	label.backgroundColor = [UIColor clearColor];
	[content addSubview:label];
	[label release];
	
	email = [[UITextField alloc]initWithFrame:CGRectMake(100, 150, 100, 20)];
	email.borderStyle = UITextBorderStyleRoundedRect;
	email.returnKeyType = UIReturnKeyDone;
	email.delegate = self;
	[content addSubview:email];
	[email release];
	//---------------------------------------------
	label = [[UILabel alloc]initWithFrame:CGRectMake(5, 195, 90, 20)];
	label.text = @"手机号";
	label.textAlignment = UITextAlignmentRight;
	
	label.font = [UIFont systemFontOfSize:13];
	label.backgroundColor = [UIColor clearColor];
	[content addSubview:label];
	[label release];
	
	cellphone = [[UITextField alloc]initWithFrame:CGRectMake(100, 195, 100, 20)];
	cellphone.borderStyle = UITextBorderStyleRoundedRect;
	cellphone.placeholder = @"必填";
	cellphone.returnKeyType = UIReturnKeyDone;
	cellphone.delegate = self;
	[content addSubview:cellphone];
	[cellphone release];
	//---------------------------------------------
	label = [[UILabel alloc]initWithFrame:CGRectMake(5, 240, 90, 20)];
	label.text = @"登录密码";
	label.textAlignment = UITextAlignmentRight;
	label.font = [UIFont systemFontOfSize:13];
	label.backgroundColor = [UIColor clearColor];
	[content addSubview:label];
	[label release];
	
	loginpassword = [[UITextField alloc]initWithFrame:CGRectMake(100, 240, 100, 20)];
	loginpassword.borderStyle = UITextBorderStyleRoundedRect;
	loginpassword.placeholder = @"必填";
	loginpassword.returnKeyType = UIReturnKeyDone;
	loginpassword.delegate = self;
	[content addSubview:loginpassword];
	[loginpassword release];
	//---------------------------------------------
	label = [[UILabel alloc]initWithFrame:CGRectMake(5, 285, 90, 20)];
	label.text = @"支付密码";
	label.textAlignment = UITextAlignmentRight;
	label.font = [UIFont systemFontOfSize:13];
	label.backgroundColor = [UIColor clearColor];
	[content addSubview:label];
	[label release];
	
	paypassword = [[UITextField alloc]initWithFrame:CGRectMake(100, 285, 100, 20)];
	paypassword.borderStyle = UITextBorderStyleRoundedRect;
	paypassword.returnKeyType = UIReturnKeyDone;
	paypassword.delegate = self;
	[content addSubview:paypassword];
	[paypassword release];
	//---------------------------------------------
	label = [[UILabel alloc]initWithFrame:CGRectMake(5, 330, 90, 20)];
	label.text = @"固定电话";
	label.textAlignment = UITextAlignmentRight;
	label.font = [UIFont systemFontOfSize:13];
	label.backgroundColor = [UIColor clearColor];
	[content addSubview:label];
	[label release];
	
	telephone = [[UITextField alloc]initWithFrame:CGRectMake(100, 330, 100, 20)];
	telephone.borderStyle = UITextBorderStyleRoundedRect;
	telephone.returnKeyType = UIReturnKeyDone;
	telephone.delegate = self;
	[content addSubview:telephone];
	[telephone release];
	//---------------------------------------------
	label = [[UILabel alloc]initWithFrame:CGRectMake(5, 375, 90, 20)];
	label.text = @"账单地址";
	label.textAlignment = UITextAlignmentRight;
	label.font = [UIFont systemFontOfSize:13];
	label.backgroundColor = [UIColor clearColor];
	[content addSubview:label];
	[label release];
	
	payaddress = [[UITextField alloc]initWithFrame:CGRectMake(100, 375, 210, 20)];
	payaddress.borderStyle = UITextBorderStyleRoundedRect;
	payaddress.returnKeyType = UIReturnKeyDone;
	payaddress.delegate = self;
	[content addSubview:payaddress];
	[payaddress release];
	//---------------------------------------------
	label = [[UILabel alloc]initWithFrame:CGRectMake(5, 420, 90, 20)];
	label.text = @"邮编";
	label.textAlignment = UITextAlignmentRight;
	label.font = [UIFont systemFontOfSize:13];
	label.backgroundColor = [UIColor clearColor];
	[content addSubview:label];
	[label release];
	
	postcode = [[UITextField alloc]initWithFrame:CGRectMake(100, 420, 100, 20)];
	postcode.borderStyle = UITextBorderStyleRoundedRect;
	postcode.returnKeyType = UIReturnKeyDone;
	postcode.delegate = self;
	[content addSubview:postcode];
	[postcode release];
	//---------------------------------------------
	label = [[UILabel alloc]initWithFrame:CGRectMake(5, 465, 90, 20)];
	label.text = @"密码保护问题1";
	label.textAlignment = UITextAlignmentRight;
	label.font = [UIFont systemFontOfSize:13];
	label.backgroundColor = [UIColor clearColor];
	[content addSubview:label];
	[label release];
	
	question1 = [[UITextField alloc]initWithFrame:CGRectMake(100, 465, 210, 20)];
	question1.borderStyle = UITextBorderStyleRoundedRect;
	question1.returnKeyType = UIReturnKeyDone;
	question1.delegate = self;
	[content addSubview:question1];
	[question1 release];
	//---------------------------------------------
	label = [[UILabel alloc]initWithFrame:CGRectMake(5, 510, 90, 20)];
	label.text = @"答案1";
	label.textAlignment = UITextAlignmentRight;
	label.font = [UIFont systemFontOfSize:13];
	label.backgroundColor = [UIColor clearColor];
	[content addSubview:label];
	[label release];
	
	answer1 = [[UITextField alloc]initWithFrame:CGRectMake(100, 510, 100, 20)];
	answer1.borderStyle = UITextBorderStyleRoundedRect;
	answer1.returnKeyType = UIReturnKeyDone;
	answer1.delegate = self;
	[content addSubview:answer1];
	[answer1 release];
	//---------------------------------------------
	label = [[UILabel alloc]initWithFrame:CGRectMake(5, 555, 90, 20)];
	label.text = @"密码保护问题2";
	label.textAlignment = UITextAlignmentRight;
	label.font = [UIFont systemFontOfSize:13];
	label.backgroundColor = [UIColor clearColor];
	[content addSubview:label];
	[label release];
	
	question2 = [[UITextField alloc]initWithFrame:CGRectMake(100, 555, 210, 20)];
	question2.borderStyle = UITextBorderStyleRoundedRect;
	question2.returnKeyType = UIReturnKeyDone;
	question2.delegate = self;
	[content addSubview:question2];
	[question2 release];
	//---------------------------------------------
	label = [[UILabel alloc]initWithFrame:CGRectMake(5, 600, 90, 20)];
	label.text = @"答案2";
	label.textAlignment = UITextAlignmentRight;
	label.font = [UIFont systemFontOfSize:13];
	label.backgroundColor = [UIColor clearColor];
	[content addSubview:label];
	[label release];
	
	answer2 = [[UITextField alloc]initWithFrame:CGRectMake(100, 600, 210, 20)];
	answer2.borderStyle = UITextBorderStyleRoundedRect;
	answer2.returnKeyType = UIReturnKeyDone;
	answer2.delegate = self;
	[content addSubview:answer2];
	[answer2 release];
	//---------------------------------------------
}


-(void)selectSex {

	UISegmentedControl *segment = (UISegmentedControl *)[self.view viewWithTag:900];
	
	if (segment.selectedSegmentIndex == 0) {
		sexSelection = @"1";
	}
	else {
		sexSelection = @"2";
	}

}
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end
